package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.utils.SendSms;
import com.itheima.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.concurrent.TimeUnit;

/**
 * @Author: ZZP
 * @Date: Created in 20:57 2022/6/4
 * @Version: 1.0
 * @Description:
 **/
@Slf4j
@RestController
@RequestMapping("/sendSmsTest")
public class SendSmsController {


    @Autowired
    private RedisTemplate redisTemplate;

//    /**
//     * 发送手机短信验证码
//     *
//     * @param user
//     * @return
//     */
//    @PostMapping("/sendMsg")
//    public R<String> sendMsg(@RequestBody User user, HttpSession session) {
//        //获取手机号
//        String phone = user.getPhone();
//
//        if (StringUtils.isNotEmpty(phone)) {
//            //生成随机的4位验证码
//            String code = ValidateCodeUtils.generateValidateCode(4).toString();
//            log.info("phone={}", phone);
//            log.info("code={}", code);
//
//            //调用阿里云提供的短信服务API完成发送短信
//            //SMSUtils.sendMessage("瑞吉外卖","",phone,code);
//
//            //需要将生成的验证码保存到Session
//            //session.setAttribute(phone,code);
//
//            //将生成的验证码缓存到Redis中，并且设置有效期为5分钟
//            redisTemplate.opsForValue().set(phone, code, 5, TimeUnit.MINUTES);
//
//            return R.success("手机验证码短信发送成功");
//        }
//
//        return R.error("短信发送失败");
//    }


    /**
     * @Author: ZZP
     * @Date: Created in 23:20 2022/6/4
     * @Version: 1.0
     * @Param: user 用户信息
     * @Return: String 验证码
     * @Description:
     */
//    @PostMapping("/sendMsg")
    public R<String> sendMsg(HttpServletRequest request, @RequestBody User user) {
        // 生成随机的4位验证码
        String validateCode = ValidateCodeUtils.generateValidateCode(4).toString();
        log.info("code={}", validateCode);
        request.getSession().setAttribute("code",validateCode);

        // 调用腾讯云提供的短信服务API
        String secretId = "AKIDHRPHxCLwCE2e2C8rNfjZCnCON8fy4xb1"; // 账户密钥对
        String secretKey = "LUO3UhkaBYgdNvHSP8c7QVMyUovpW0HL"; // 账户密钥对
        String signNamePOwn = "快乐篮球"; // 短信签名
        String templateIdOwn = "1427990"; // 模板ID
        String sdkAppIdOwn = "1400688855"; // 短信应用ID
        String[] phoneNumber = {user.getPhone()}; // 手机号
        String[] templateParam = {validateCode}; // 模板参数
        SendSms.sendMessage(secretId, secretKey, signNamePOwn, templateIdOwn, sdkAppIdOwn, phoneNumber, templateParam);

        return R.success("手机验证码短信发送成功");
    }
}
